
// tgweb: "https://game.sidekick.fans/#tgWebAppData=query_id%3DAAHbJAxcAwAAANskDFw2e4QZ%26user%3D%257B%2522id%2522%253A7986750683%252C%2522first_name%2522%253A%2522Andrew%2520Joel%2522%252C%2522last_name%2522%253A%2522%2522%252C%2522language_code%2522%253A%2522en%2522%252C%2522allows_write_to_pm%2522%253Atrue%257D%26auth_date%3D1729174070%26hash%3D754716ccdaa424f07edfd9c248e5983b81ac176e039b5a60c911032f581e0aee&tgWebAppVersion=7.7&tgWebAppPlatform=ios"

export default class TgwebParser {
  constructor(tgweb) {
    this.tgweb = tgweb
    let urlStr = decodeURIComponent(tgweb)
    urlStr = urlStr.replace("#tgWebAppData=", "?")
    this.tgwebURL = new URL(urlStr)
    this.searchParams = this.tgwebURL.searchParams
  }

  getParams(name) {
    return (this.searchParams || {}).get(name)
  }

  getUser() {
    let userStr = (this.searchParams || {}).get("user")
    userStr = decodeURIComponent(userStr)
    let userObj = JSON.parse(userStr)
    return userObj
  }

  getSession() {
    let webSessionUrl = this.tgweb
    if (webSessionUrl.startsWith("https://") == false) {
      console.error(`${this.gameInfo.phone}  error session url ${webSessionUrl}`);
      return null
    }
    webSessionUrl = decodeURIComponent(webSessionUrl)
    let session = webSessionUrl.split("#tgWebAppData=")[1]
    session = session.split("&tgWebAppVersion")[0]
    return session
  }
}